//
//  DDImagePickerController.m
//  PrivateCloud
//
//  Created by mars on 13-11-7.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDImagePickerController.h"
#import "DDImagePickerCell.h"
#import "UIColor+.h"
#import "DDImagePickerModel.h"
#import "DDFileInfo.h"
#import "DDSuccessViewController.h"
#import "DDVerifyThread.h"
#include <AssetsLibrary/AssetsLibrary.h>
#define ALL_SELECT_TAG 10000
@implementation DDImagePickerController

@synthesize table,resultArray,fileDataSource,isAllSelect,progressHUD;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //FIX ME 目前返回一个固定值
    return [resultArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cellID";
    DDImagePickerCell *cell = [[[DDImagePickerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    cell.model = [self.resultArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    DDImagePickerModel *model = [self.resultArray objectAtIndex:indexPath.row];
    model.isSelected = !model.isSelected;
    [self.resultArray removeObjectAtIndex:indexPath.row];
    [self.resultArray insertObject:model atIndex:indexPath.row];

    
    [tableView reloadData];
}

- (void) viewDidLoad{
    [super viewDidLoad];
    self.table = [[UITableView alloc] initWithFrame:CGRectMake( 5, 60, self.view.frame.size.width - 10, self.view.frame.size.height - 120) style:UITableViewStylePlain];
    self.table.delegate = self;
    table.dataSource = self;
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
    //将DDFileInfo转换为当前的model对象
    self.resultArray = [[NSMutableArray alloc]init];
    if ([self.fileDataSource count]>0) {
        for (DDFileInfo* fileInfo in self.fileDataSource) {
            [assetsLibrary assetForURL:[fileInfo url] resultBlock:^(ALAsset *asset) {
                
                DDImagePickerModel* model = [[DDImagePickerModel alloc]init];
                model.uid = [fileInfo fileId];
                model.path = [fileInfo filePath];
                model.url = [fileInfo url];
                model.asset = asset;
                [self.resultArray addObject:model];
                
                [model release];
                [table reloadData];
            } failureBlock:^(NSError *error) {
            }];
            
        }
    }
    
    [self createTopView];
    
    
    [self.view addSubview:table];
}

- (void)createTopView{
    
    UIView *linkView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
    //    linkView.backgroundColor = [UIColor greenColor];
    linkView.backgroundColor = [UIColor colorWithString:@"#273826"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"ico_back.png"];
    backButton.frame = CGRectMake(10, 5, 32,32);
//    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [backButton addTarget:self action:@selector(backbuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:image forState:UIControlStateNormal];
    [linkView addSubview:backButton];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-25, 5, self.view.frame.size.width - image.size.width - 80, 30)];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"图片";
    label.textColor = [UIColor whiteColor];
    [linkView addSubview:label];
    [label release];
    [self.view addSubview:linkView];
    
    UIButton *allSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    allSelectButton.frame = CGRectMake(self.view.frame.size.width - 50, 0, 80, 30);
    allSelectButton.backgroundColor = [UIColor clearColor];
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, allSelectButton.frame.size.width, allSelectButton.frame.size.height)];
    buttonLabel.textColor = [UIColor whiteColor];
    buttonLabel.font = [UIFont systemFontOfSize:15];
    buttonLabel.backgroundColor = [UIColor clearColor];
    buttonLabel.tag = ALL_SELECT_TAG;
    buttonLabel.text = @"全选";
    [allSelectButton addTarget:self action:@selector(selectAllItem:) forControlEvents:UIControlEventTouchUpInside];
    [allSelectButton addSubview:buttonLabel];
    [buttonLabel release];
    [linkView addSubview:allSelectButton];
    [linkView release];
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    [footerView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:footerView];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
    sureButton.backgroundColor = [UIColor clearColor];
    UILabel *sureLabel = [[UILabel alloc] initWithFrame:CGRectMake(footerView.frame.size.width/2 - 25, 0, allSelectButton.frame.size.width, allSelectButton.frame.size.height)];
    sureLabel.textColor = [UIColor whiteColor];
    sureLabel.font = [UIFont systemFontOfSize:15];
    sureLabel.backgroundColor = [UIColor clearColor];
    sureLabel.text = @"确   定";
    [sureButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sureButton addSubview:sureLabel];
    [sureLabel release];
    [footerView addSubview:sureButton];
    [footerView release];
    
}

-(void)sureButtonPressed:(id)sender{
    NSMutableArray* selectedFileinfos = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < [self.resultArray count]; i++) {
        DDImagePickerModel* model = [self.resultArray objectAtIndex:i];
        if(model.isSelected){
            [selectedFileinfos addObject:[self.fileDataSource objectAtIndex:i]];
        }
    }
    
    if([selectedFileinfos count] == 0){
        [self showSelectAlert:LOCAL_CODE];
        return;
    }
    
    if ([selectedFileinfos count]>0) {
            //开始执行上传操作
            [[DDVerifyThread sharedVerifyThread]uploadFileList:selectedFileinfos];
            [self showProgressDialogWithCode:LOCAL_CODE];
            [DDVerifyThread sharedVerifyThread].dealFileCallback = self;
    }
}

-(void)selectAllItem:(id)sender{
    UILabel* buttonLabel = (UILabel*)[self.view viewWithTag:ALL_SELECT_TAG];
    if(isAllSelect == YES){
        //取消全选
        for (DDImagePickerModel* model in self.resultArray) {
            model.isSelected = NO;
        }
        isAllSelect = NO;
        buttonLabel.text = @"全选";
    }
    else{
        //全选
        for (DDImagePickerModel* model in self.resultArray) {
            model.isSelected = YES;
        }
        isAllSelect = YES;
        buttonLabel.text = @"取消";
    }
    [self.table reloadData];
}

-(void)copyImageToLocation:(ALAsset*) asset{
    
}

- (void)backbuttonPressed:(id)sender {
    DDSuccessViewController* successController = [[DDSuccessViewController alloc]init];
    [self presentViewController:successController animated:NO completion:nil];
    [successController release];
}

-(void)showSelectAlert:(int)code{
    NSString* labelString = nil;
    if(code == SERVER_CODE){
        labelString = @"请选择需要下载的文件";
    }
    else{
        labelString = @"请选择需要上传的文件";
    }
    progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:progressHUD];
    
    progressHUD.mode = MBProgressHUDModeCustomView;
    progressHUD.labelText= labelString;
    [progressHUD show:YES];
    [progressHUD hide:YES afterDelay:3];
}

#pragma DDDealFileCallback
-(void)onProgressChanged:(int)progress{
    float trueProgress = progress/100.0f;
    progressHUD.progress = trueProgress;
    NSLog(@"VIEW----------%f",trueProgress);
}

-(void)onDealFileComplete:(DDFileInfo *)info andCode:(int)code{
    [self dismisProgressDialog];
}



- (void)showProgressDialogWithCode:(int)code{
    NSString* labelString = nil;
    if(code == SERVER_CODE){
        labelString = @"正在下载数据..";
    }
    else{
        labelString = @"正在上传数据..";
    }
    progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    progressHUD.mode = MBProgressHUDModeDeterminate;
    progressHUD.opacity = 0.6;
    progressHUD.labelFont = [UIFont boldSystemFontOfSize:14];
    progressHUD.detailsLabelFont = [UIFont boldSystemFontOfSize:12];
    progressHUD.detailsLabelText = labelString;
    progressHUD.removeFromSuperViewOnHide = YES;
}

- (void)dismisProgressDialog{
    progressHUD.hidden = YES;
    //    [progressHUD release];
}

@end
